package com.cccyy.view;

import org.jcodec.api.FrameGrab;
import org.jcodec.api.JCodecException;
import org.jcodec.common.model.Picture;
import org.jcodec.scale.AWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class VideoPreview {

    private static final Logger logger = LoggerFactory.getLogger(VideoPreview.class);

    public static void main(String[] args) {
        try {
            String videoPath = "D:/data/video/1710932891438_33qTz.mp4";
            long l = System.currentTimeMillis();
            String previewImagePath = "D:/data/preview/" + l + "_preview.jpg";
            // 视频文件路径
            File videoFile = new File(videoPath);

            // 获取视频的第一帧作为预览图
            Picture frame = FrameGrab.getFrameFromFile(videoFile, 1);

            // 将视频帧转换为BufferedImage
            BufferedImage image = AWTUtil.toBufferedImage(frame);

            // 保存生成的预览图
            File outputImageFile = new File(previewImagePath);
            ImageIO.write(image, "jpg", outputImageFile);

            logger.info("预览图生成成功：" + outputImageFile.getAbsolutePath());
        } catch (IOException e) {
            logger.error(e.getMessage());
        } catch (JCodecException e) {
            throw new RuntimeException(e);
        }

    }
}
